var usercheck = 0;
$(function()
{
	//$("#tablelogin").draggable();
	$('#username').button().css({'cursor': 'text','text-align': 'left','width': '180','height': '34'});
	$('#password').button().css({'cursor': 'text','text-align': 'left','width': '180','height': '34'});
	$('#submit').button();
	$('#register').button();
	checklogin();
});

function login()
{
	if($('#username').val()==''&&$('#password').val()=='')
	{
		$('#dialog').html('Plase enter username and password');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#username').val()=='')
	{
	$('#dialog').html('Plase enter the username');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#password').val()=='')
	{
	$('#dialog').html('Plase enter the password');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqlogin.php',{ 'username': $('#username').val(),'password': $('#password').val()},function(data)
		{
			if(data == 0)
			{
				$('#dialog').html('Username or Password incorrect');
				$('#dialog').dialog({'title':'Error'});
				$('#dialog').dialog('option','resizable',false);
				$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
			}else if (data == 1)
			{
				$.get('ui/mainAdmin.php',function(data)
				{
					$('#main').html(data);
					$('#html_title').html('Course Planing Admin');
				});			
				}else if (data > 1)
			{
				$.get('ui/mainUser.php',function(data)
				{
					$('#main').html(data);
					$('#html_title').html('Course Planing');
				});	
			}else
			{
				$('#dialog').html(data);
				$('#dialog').dialog({'title':'Error'});
				$('#dialog').dialog('option','resizable',false);
				$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
			}
		});
	}
	
}

function register()
{
	$.get('ui/register.php',function(data)
	{
		$('#main').html(data);
		$('#html_title').html('Register');
	});
}

function checklogin()
{
	$.get('class/_reqcheck.php',{},function(data)
		{
			if (data == 1)
			{
				$.get('ui/mainAdmin.php',function(data)
				{
					$('#main').html(data);
					$('#html_title').html('Course Planing Admin');
				});			
			}else if (data > 1)
			{
				$.get('ui/mainUser.php',function(data)
				{
					$('#main').html(data);
					$('#html_title').html('Course Planing');
				});		
			}else
			{
				/*$.get('ui/login.php',function(data)
				{
					$('#main').html(data);
					$('#html_title').html('Course Planing');
				});*/
			}
		});
}

function logoutcon()
{
	$('#outcon').html('You want to Logout?');
		$('#outcon').dialog({ buttons: [{text: 'Logout',click: function()
		{ 
			logout();
			$( this ).dialog('close'); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
		$('#outcon').dialog({'title':'Confirm Logout'});
		$('#outcon').dialog('option','resizable',false);
}

function logout()
{
	$.get('class/_reqlogout.php',{},function(data)
		{
			window.usercheck = 0;
			$.get('ui/login.php',function(data)
			{
				$('#main').html(data);
				$('#html_title').html('Course Planing');
			});			
		});
}